import { _decorator, Collider2D, Component, Contact2DType, IPhysics2DContact, math, Node, v2, Vec2 } from 'cc';
import { PlayerState } from './PlayerState';
import { Player } from './Player';
import { PlayerStateMachine } from './PlayerStateMachine';
import { Entity } from '../Entity';
import { EnemyManager } from '../EnemyManager';
const { ccclass, property } = _decorator;

@ccclass('PlayerAttackState')
export class PlayerAttackState extends PlayerState {
    private weaponCollider: Collider2D | null = null;
    velocity: Vec2 = v2();


    constructor(_player: Player, _stateMachine: PlayerStateMachine) {
        super(_player, _stateMachine);
    }

    override enter(): void {
        super.enter();

        this.player.skeleton.setCompleteListener((trackEntry) => {
            this.stateMachine.changeState(this.player.idleState);
        });
        this.weaponCollider = this.player.weapon.getComponent(Collider2D);
        if (this.weaponCollider) {
            this.weaponCollider.enabled = true;
        }
    }

    override update(dt: number): void {
        super.update(dt);
        this.move();
       
    }

    override exit(): void {
        super.exit();
        if (this.weaponCollider) {
            this.weaponCollider.enabled = false;
        }
    }


    private move() {
        this.velocity.set(this.player.input.x, this.player.input.y);
        this.velocity.multiplyScalar(this.player.moveSpeed);
        this.rb.linearVelocity = this.velocity;
    }


}
